package com.hepinghospital.user.ui.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;

import androidx.annotation.NonNull;


/**
 * @time 2020/11/18 11:43
 * Created by lbj on 2020/11/18 11:43.
 */
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    /**
     * 构造函数 默认圆角半径 4dp
     */
    public GlideRoundTransform() {
        this( 4 );
    }

    /**
     * 构造函数
     *
     * @param dp 圆角半径
     */
    public GlideRoundTransform(int dp) {
        super();
        radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        //变换的时候裁切
        Bitmap bitmap = TransformationUtils.centerCrop( pool, toTransform, outWidth, outHeight );
        return roundCrop( pool, bitmap );
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        Bitmap result = pool.get( source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888 );
        if (result == null) {
            result = Bitmap.createBitmap( source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888 );
        }
        Canvas canvas = new Canvas( result );
        Paint paint = new Paint();
        paint.setShader( new BitmapShader( source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP ) );
        paint.setAntiAlias( true );
        RectF rectF = new RectF( 0f, 0f, source.getWidth(), source.getHeight() );
        canvas.drawRoundRect( rectF, radius, radius, paint );
        //左上角、右上角圆角
        //        RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
        //        canvas.drawRect(rectRound, paint);
        return result;
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}
